package com.lugea.common.aop;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author enluw
 * @date 2020/1/25
 * @description 与apiMthod组合使用
 */
@Aspect
@Component
@Order(1)
public class ApiMethodAspect {

    private static String hostAddress = null;
    static {
        InetAddress localHost = null;
        try {
            localHost = InetAddress.getLocalHost();
            hostAddress = localHost.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @Pointcut("@annotation(com.lugea.common.annotations.ApiMethod)")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before() throws Throwable {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        MDC.put("traceId", uuid);
    }

    @After("pointCut()")
    public void doAfter() {
        MDC.remove("traceId");
    }
}
